﻿using System;
using System.IO;
using System.Collections.ObjectModel;
using System.Text;

using System.Xml;
using System.Xml.Serialization;
using cz.uhul.DSMLv2;

namespace testDSML
{
  public class testRequest
  {

    public static void go()
    {
      LdRequestDocument r = new LdRequestDocument()
      {
        DocumentProcessing = LdDocumentProcessing.Parallel
        ,ResponseOrder = LdResponseOrderEnum.Unordered
        ,AuthRequest = new LdAuthRequest() { Principal = "tst" }
        //AddRequests = new LdAddRequestCollection()
        ,AddRequests = new LdRequestsCollection()
        {
          new LdAddRequest()
          {
            DistinguishedName="CN=Alice,OU=HR,DC=Example,DC=COM"
            ,Attributes = new LdAttrCollection()
            {
              new LdAttr()
              {
                Name = "objectClass"
                ,Values = new ObservableCollection<string>()
                {
                  "person"
                }
              }
              ,new LdAttr()
              {
                Name = "uid"
                ,Values = new ObservableCollection<string>()
                {
                  "alice"
                }
              }
            }
          }
          ,new LdAddRequest()
          {
            DistinguishedName="CN=Bob,OU=HR1,DC=Example,DC=COM"
            ,Attributes = new LdAttrCollection()
            {
              new LdAttr()
              {
                Name = "objectClass"
                ,Values = new ObservableCollection<string>()
                {
                  "person"
                }
              }
              ,new LdAttr()
              {
                Name = "uid"
                ,Values = new ObservableCollection<string>()
                {
                  "bob"
                }
              }
            }
          }
        }
        ,DeleteRequests = new LdRequestsCollection()
        {
          new LdDeleteRequest()
          {
            DistinguishedName="CN=Alice,OU=HR,DC=Example,DC=COM"
          }

          ,new LdDeleteRequest()
          {
            DistinguishedName="OU=HR,DC=Example,DC=COM"
            ,Controls = new LdControlCollection()
            {
              new LdControl()
              {
                Type = "1.2.840.113556.1.4.805"
              }
            }
          }
        }
        ,
        //ModifyRequests = new LdModifyRequestCollection()
        ModifyRequests = new LdRequestsCollection()
        {
          new LdModifyRequest()
          {
            DistinguishedName="CN=Bob,OU=HR1,DC=Example,DC=COM"
            ,Attributes = new LdModifyAttrCollection()
            {
              new LdModifyAttr()
              {
                Name = "uid"
                ,Values = new ObservableCollection<string>()
                {
                  "bob-d"
                }
                ,Operation = LdAttrModifyOperationEnum.replace
              }
            }
          }
          ,new LdModifyRequest()
          {
            DistinguishedName="CN=Bob,OU=HR1,DC=Example,DC=COM"
            ,Attributes = new LdModifyAttrCollection()
            {
              new LdModifyAttr()
              {
                Name = "uid"
                ,Values = new ObservableCollection<string>()
                {
                  "bob-b"
                }
                ,Operation = LdAttrModifyOperationEnum.replace
              }
            }
          }
        } //end of ModifyRequests
        ,
        SearchRequests = new LdRequestsCollection()
        {
          new LdSearchRequest()
          {
            DistinguishedName="OU=HR1,DC=Example,DC=COM"
            ,DerefAliases = LdDerefAliasesEnum.neverDerefAliases
            ,Scope = LdSearchScopeEnum.wholeSubtree
            ,TypesOnly = false

            ,Filter = new FilterGroup()
            {
              And = new AndOp()
              {
                Presents = new ObservableCollection<FilterOp>()
                {
                  new PresentOp()
                  {
                    Name = "uid"
                  }
                }
                ,Substrings = new ObservableCollection<FilterOp>()
                {
                  new SubstringsOp()
                  {
                    Name = "uid"
                    ,Initial = "B"
                  }
                }
              }
            }
          }
        }


      };

      string t;

      //if (r.AddRequests.Count > 0)
      //  t = r.AddRequests[0].ObjectClass[r.AddRequests[0].ObjectClass.Count - 1];


      LdResponseDocument response = new LdResponseDocument();

      XmlDocument doc = r.ToXml();

      Console.WriteLine("request**************************************");
      Console.WriteLine(doc.InnerXml.ToString());

      LdRequestDocument tst = LdRequestDocument.Parse(doc);
      //if (r.AddRequests != null)
      //  foreach (LdAddRequest rq in r.AddRequests)
      //    response.AddResponses.Add(rq.Execute());


      Console.WriteLine("parsed request**************************************");
      Console.WriteLine(tst.ToXml().InnerXml.ToString());

      Console.WriteLine("response**************************************");
      Console.Write(response.ToXml().OuterXml.ToString());






    }







  }

}
